package com.huang825172.sbog.Tabs.Mine.RegisterPage.View;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huang825172.sbog.BaseFlux.Dispatcher;
import com.huang825172.sbog.BaseFlux.Store.IDataChangedListener;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Mine.RegisterPage.Action.ActionCreator;
import com.huang825172.sbog.Tabs.Mine.RegisterPage.Store.Store;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterPage extends AppCompatActivity implements View.OnClickListener, IDataChangedListener {

    @BindView(R.id.RegisterPageConfirmButton)
    Button confirmButton;
    @BindView(R.id.RegisterPagePhoneEditText)
    EditText phoneEditText;
    @BindView(R.id.RegisterPagePWEditText)
    EditText pwEditText;
    @BindView(R.id.RegisterPageCheckEditText)
    EditText checkEditText;

    @BindView(R.id.RegisterPageSendCheckTextView)
    TextView sendCheckTextView;

    private Store store;
    private ActionCreator actionCreator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_page);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        this.store = new Store(getApplicationContext());
        this.store.setDataChangedListener(this);
        Dispatcher.getSingleton().register(this.store);
        this.actionCreator = new ActionCreator();

        confirmButton.setOnClickListener(this);
        sendCheckTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.RegisterPageConfirmButton: {
                String phone = phoneEditText.getText().toString();
                String password = pwEditText.getText().toString();
                String check = checkEditText.getText().toString();
                actionCreator.createRegisterAction(phone, password, check);
                break;
            }
            case R.id.RegisterPageSendCheckTextView: {
                actionCreator.createSendCheckAction();
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onDataChanged(String label) {
        switch (label) {
            case "RegisterPageRegisterAction": {
                Toast.makeText(getApplicationContext(), store.getRegisterResponseRaw(), Toast.LENGTH_SHORT).show();
                break;
            }
            case "RegisterPageSendCheckAction": {
                Toast.makeText(getApplicationContext(), "Check Sended", Toast.LENGTH_SHORT).show();
                break;
            }
            default:
                break;
        }
    }
}
